package com.example.demo.service.Impl;

import com.example.demo.entity.Image;
import com.example.demo.mapper.SelectImage;
import com.example.demo.service.Select;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class SelectImpl implements Select {

    SelectImage selectimage=new SelectImage();

//    图片查询
    @Override
    public List<Image> select() {
        List<Image> list=new ArrayList<>();
        try{
            list = selectimage.query();
        }catch(Exception e) {
         e.printStackTrace();
        }
        return list;
    }

//    图片添加
    @Override
    public boolean addhttpImage(String Image, String originalImage, Long type, String title) {
        Image image=new Image();
        image.setImagehttps(Image);
        image.setFormerImagehttp(originalImage);
        image.setTitle(title);
        image.setType(type);
        try{
            selectimage.addImage(image);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }


    //    图片存储（本地）
    @Override
    public Boolean addlocalImage(MultipartFile file, MultipartFile originalfile, Long type, String title) throws Exception{

//        图片后缀
        String ss=file.getOriginalFilename().substring(file.getOriginalFilename().length(),file.getOriginalFilename().length()-4);
        String ss2=originalfile.getOriginalFilename().substring(originalfile.getOriginalFilename().length(),originalfile.getOriginalFilename().length()-4);

        String fileName=file.getOriginalFilename().substring(0,file.getOriginalFilename().length()-4)+ UUID.randomUUID().toString()+ss;
        String originalfileName=originalfile.getOriginalFilename().substring(0,originalfile.getOriginalFilename().length()-4)+UUID.randomUUID().toString()+ss2;
        //        路径
        String firstPath = "C:\\Image";
        String originalPath="C:\\originalImage";


//        文件存储
        File tempFile = new File(firstPath, fileName);
        File temporiginalFile =new File(originalPath,originalfileName);

        if (fileName != "" && !file.isEmpty()){
//            检测是否存在目录
            if (!tempFile.getParentFile().exists()) {
//                创建目录
                tempFile.getParentFile().mkdirs();
            }
//            存入文件
            file.transferTo(tempFile);
        }

        if(originalfileName!=""&&!originalfile.isEmpty()){
            if(!temporiginalFile.getParentFile().exists()){
                temporiginalFile.getParentFile().mkdirs();
            }
            originalfile.transferTo(temporiginalFile);
        }
        //图片地址名
        String addFile=firstPath+"\\" +fileName;
        String addoriginalFile=originalPath+"\\"+originalPath;
        Image image=new Image();
        image.setImagehttps(addFile);
        image.setFormerImagehttp(addoriginalFile);
        image.setTitle(title);
        image.setType(type);
        try{
            selectimage.addImage(image);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;


    }

//    更改信息
    @Override
    public boolean updataImage(Image newImage, Image oldImage) {
        try {
            selectimage.updata(newImage, oldImage);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }


    //    图片删除(在本地)
    @Override
    public boolean delecthttpImage(String addrImage, String addroriginalfile) {
        try {
            //删除本地图片
            File deleteaddrImage=new File(addrImage);
            File deleteaddroriginalfile=new File(addroriginalfile);
            deleteaddrImage.delete();
            deleteaddroriginalfile.delete();

//            删除数据库内容
            Image image=new Image();
            image.setImagehttps(addrImage);
            image.setFormerImagehttp(addroriginalfile);
            selectimage.deleteImage(image);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //    图片删除（只有链接）
    @Override
    public boolean delectlocalImage(String addrImage, String addroriginalfile) {
        try{
            Image image=new Image();
            image.setImagehttps(addrImage);
            image.setFormerImagehttp(addroriginalfile);
            selectimage.deleteImage(image);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

//    模糊查询
    @Override
    public List<Image> vagueImageSelect(String title) {
        List<Image> list=new ArrayList<>();
        try{
            list = selectimage.vagueImageSelect(title);
        }catch(Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    //    图片下载
    @Override
    public boolean downloadImage(String urlString, String filename) {
        try {
            selectimage.downloadImage(urlString, filename);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }




}
